package tw.twandroidproj.app.ui.resume;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.util.Calendar;

import tw.twandroidproj.app.R;
import tw.twandroidproj.app.ui.personal.SelectPicPopupWindow;

public class Resume_create extends AppCompatActivity {

    TextView resume_workarea_value_info,resume_graduation_value_info;
    TextView resume_touxiang;
    static String nocitystring,schoolname;
    SelectPicPopupWindow menuWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resume_create);

        TextView resume_titlebar_center=(TextView)findViewById(R.id.resume_titlebar_center);
        resume_workarea_value_info=(TextView)findViewById(R.id.resume_workarea_value_info);
        resume_graduation_value_info=(TextView)findViewById(R.id.graduation_value_info);
        final TextView birthday_value_info=(TextView)findViewById(R.id.birthday_value_info);
        Button resume_titlebar_right=(Button)findViewById(R.id.resume_titlebar_right);
        Button resume_titlebar_left=(Button)findViewById(R.id.resume_titlebar_left);
        final Button resume_sex_check_male=(Button)findViewById(R.id.male_info);
        final Button resume_sex_check_female=(Button)findViewById(R.id.female_info);

        resume_touxiang=(TextView) findViewById(R.id.im_camera);
        RelativeLayout resume_workarea=(RelativeLayout)findViewById(R.id.resume_workarea_info);
        RelativeLayout birthday_info=(RelativeLayout)findViewById(R.id.birthday_info);
        RelativeLayout school_name_check=(RelativeLayout)findViewById(R.id.graduation_info);

        resume_titlebar_center.setText("创建简历");
        resume_titlebar_right.setText("完成");

        resume_titlebar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        resume_titlebar_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        resume_touxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuWindow = new SelectPicPopupWindow(Resume_create.this,itemsOnClick);
                menuWindow.showAtLocation(resume_touxiang, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });

        resume_sex_check_male.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resume_sex_check_male.setBackgroundResource(R.drawable.btn_sex_left_c);
                resume_sex_check_female.setBackgroundResource(R.drawable.btn_sex_left_n);
            }
        });

        resume_sex_check_female.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resume_sex_check_female.setBackgroundResource(R.drawable.btn_sex_left_c);
                resume_sex_check_male.setBackgroundResource(R.drawable.btn_sex_left_n);
            }
        });

        birthday_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar date =Calendar.getInstance();
                new DatePickerDialog(Resume_create.this,
                        new DatePickerDialog.OnDateSetListener()
                        {
                            @Override
                            public void onDateSet(DatePicker dp,int year,int month,int dayOfMonth)
                            {
                                birthday_value_info.setText(year+"年"+(month+1)+"月"+dayOfMonth+"日");
                            }
                        }
                        ,date.get(Calendar.YEAR),date.get(Calendar.MONTH),date.get(Calendar.DAY_OF_MONTH)).show();

            }
        });

        resume_workarea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Resume_create.this, Resume_citys.class);
                startActivityForResult(intent, 1);
            }
        });
        resume_workarea_value_info.setText(getIntent().getStringExtra("textViewLabel"));

        school_name_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_edit_school=new Intent(Resume_create.this,Resume_school_check.class);
                startActivityForResult(intent_edit_school,1);
            }
        });
        resume_graduation_value_info.setText(getIntent().getStringExtra("schoolname"));

    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener(){

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_take_photo:takePhoto();
                    break;
                case R.id.btn_pick_photo:getPhoto();
                    break;
                default:
                    break;
            }


        }

    };
    private void getPhoto() {
        Intent intent=new Intent();
        intent.setType("image/*");
        //intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 4);



    }
    private Uri getUri(){
        String path;
        path= Environment.getExternalStorageDirectory().getPath();
        path=path+"/"+"tem.png";
        File file=new File(path);
        Uri uri= Uri.fromFile(file);//资源定位
        return uri;
    }
    public void  takePhoto(){

        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0);
        startActivityForResult(intent, 3);
    }

    public   void cropPhoto(Uri uri){
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");//?
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent,5);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {

            switch (resultCode) {
                case 1:
                    super.onActivityResult(requestCode, resultCode, data);
                    //接收数据：采用Bundle传值
                    Bundle bundle_workarea = data.getExtras();
                    nocitystring = bundle_workarea.getString("textViewLabel");
                    resume_workarea_value_info.setText(nocitystring);
                    break;
                case 2:
                    super.onActivityResult(requestCode, resultCode, data);
                    //接收数据：采用Bundle传值
                    Bundle bundle_school = data.getExtras();
                    schoolname = bundle_school.getString("schoolname");
                    resume_graduation_value_info.setText(schoolname);
                    break;
                case RESULT_OK:
                {
                    switch (requestCode) {
                        case 3:
                            cropPhoto(getUri());
                            break;
                        case 4:
                            cropPhoto(data.getData());
                            break;
                        case 5:
                            Bundle bundle = data.getExtras();
                            Bitmap bitmap = bundle.getParcelable("data");
                            if (bitmap != null) {
                                Drawable drawable = new BitmapDrawable(bitmap);
                                //drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                                drawable.setBounds(0, 0, 200, 200);
                                resume_touxiang.setCompoundDrawables(null, null, drawable, null);
                            }
                            break;
                    }
                    super.onActivityResult(requestCode, resultCode, data);
                }

                default:
                    break;
            }

        //super.onActivityResult(requestCode, resultCode, data);
    }

}
